<?php


namespace app\admin\controller;
use app\common\controller\Admin;
use app\ads\controller\Api;


class Replaceversion extends Admin
{
    public function _initialize() {
        parent::_initialize();

    }

    public function _empty(){
        $action = ACTION_NAME;

        $list = db('replace_log');
        $uid   = session('user_auth.uid');
        if($uid>1){
            $list = $list->where('uid', $uid);
        }
        $list = $list->order('create_time', 'desc')->paginate( 30, false, [ 'type'=> 'page\Page' ,'query' => request()->param()]);
        $page = $list->render();
        $this->assign(compact('list', 'page'));
        $this->setMeta("版本替换");
        return view('replaceversion/'.$action );
    }

    /**
     * Notes: 文件替换
     * @author: Zhu
     * @since: 2022/12/5 10:16
     */
    public function replace()
    {
        if (IS_POST) {
            $params = input('post.');
            $path =  'uploads'.DIRECTORY_SEPARATOR.'attachment'.DIRECTORY_SEPARATOR.date('Ymd').DIRECTORY_SEPARATOR;
            $path = str_replace('\\', '/', $path);
            $i = 0;
            $list = [];
            foreach ($params['replace'] as $k => $v) {
                $file = file_get_contents($params['file_path']);
                $encoding = mb_detect_encoding($file, array('GB2312','GBK','UTF-16','UCS-2','UTF-8','BIG5','ASCII'));
                if ($encoding != 'UTF-8') {
                    $file = iconv($encoding, "UTF-8", $file);
                }
                $replaceList = explode(',', $v);
                foreach (explode(',', $params['search']) as $kk => $vv) {
                    $file = str_replace($vv, $replaceList[$kk], $file);
                }
                $file_out = $path.date('YmdHis', time()).$i.".txt";
                if (!is_dir(ROOT_PATH . $path)) {
                    mkdir($path, 0755, true);
                }

                file_put_contents($file_out, $file);
                $name = $params['name'] ?? basename($params['file_path']);
                $name = str_replace(strrchr($name, '.'), '', $name);
                $bookid = isset($params['id']) ? db('upload_txt_log')->where('id', $params['id'])->value('book_id') : 0;
                $myfile = file($file_out);
                $content = '';
                for ($i = 0; $i < 100; $i++) {
                    $content .= $myfile[$i] . '<br>';
                }

                $list[] = [
                    'txt_log_id' => $params['id'] ?? 0,
                    'name' => $name . '-' . explode(',', $params['search'])[0] . '-' . $replaceList[0] . '.txt',
                    'search' => $params['search'],
                    'replace' => $params['replace'][$k],
                    'replace_keyword' => $replaceList[0],
                    'file' => $params['file_path'],
                    'book_id' => $bookid,
                    'file_out' => $file_out,
                    'uid' => session('user_auth.uid'),
                    'content' => $content,
                    'create_time' => time()
                ];
                $i++;
            }
            $replace = db('replace_log')->insertAll($list);
            if ($replace) {
                return $this->success("成功！");
            } else {
                return $this->error("失败！");
            }
        }else{
            return $this->fetch();
        }
    }

}